#!/bin/bash
#
# Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only
#

CURDIR=$(dirname "$0")/
TESTDIR="$CURDIR/.."
LAUNCH_APP="launch_ust_app"
KILL_RELAYD_HELPER="kill_relayd"
SESSION_NAME="stress"
EVENT_NAME="tp:tptest"
LOG_FILE_SESSIOND="sessiond.log"
LOG_FILE_RELAYD="relayd.log"
CHANNEL_NAME="channel0"
NR_APP=5
NR_SESSION=5
NR_LOOP=100000
COREDUMP_FILE=$(cat /proc/sys/kernel/core_pattern)
NUM_TESTS=16
APP_PIDS=()

TEST_DESC="Stress test - $NR_SESSION sessions per UID streaming with $NR_APP apps. The relayd is killed sporadically"

# shellcheck source-path=SCRIPTDIR/../
source "$TESTDIR/utils/utils.sh"

# MUST set TESTDIR before calling those functions

function enable_channel_per_uid()
{
	local sess_name=$1
	local channel_name=$2

	"$TESTDIR/../src/bin/lttng/$LTTNG_BIN" enable-channel --buffers-uid -u "$channel_name" -s "$sess_name" >/dev/null 2>&1
	ok $? "Enable channel $channel_name per UID for session $sess_name"
}

function check_sessiond()
{
	local str_date
	if [ -z "$(lttng_pgrep lttng-sessiond)" ]; then
		str_date=$(date +%H%M%S-%d%m%Y)

		diag "!!!The session daemon died unexpectedly!!!"
		mv $LOG_FILE_SESSIOND "$LOG_FILE_SESSIOND-$str_date"
		if [ -e "$COREDUMP_FILE" ]; then
			mv "$COREDUMP_FILE" "$COREDUMP_FILE-$str_date"
		fi
		exit 1
	fi
}

function start_sessiond()
{
	if ! validate_kernel_version ; then
		fail "Start session daemon"
		BAIL_OUT "*** Kernel too old for session daemon tests ***"
	fi

	if [ -z "$(lttng_pgrep $SESSIOND_BIN)" ]; then
		# We have to start it like this so the ulimit -c is used by this
		# process. Also, we collect any error message printed out.
		#$TESTDIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --quiet --background --consumerd32-path="$TESTDIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$TESTDIR/../src/bin/lttng-consumerd/lttng-consumerd" >$LOG_FILE_SESSIOND 2>&1
		"$TESTDIR/../src/bin/lttng-sessiond/$SESSIOND_BIN" --verbose-consumer -vvv --background --consumerd32-path="$TESTDIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$TESTDIR/../src/bin/lttng-consumerd/lttng-consumerd" >$LOG_FILE_SESSIOND 2>&1
		#$TESTDIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --background --consumerd32-path="$TESTDIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$TESTDIR/../src/bin/lttng-consumerd/lttng-consumerd" >$LOG_FILE_SESSIOND 2>&1
		status=$?
		ok $status "Start session daemon"
	fi
}

function start_relayd
{
	local opt=$1

	if [ -z "$(lttng_pgrep $RELAYD_BIN)" ]; then
		"$TESTDIR/../src/bin/lttng-relayd/$RELAYD_BIN" "$opt" >$LOG_FILE_RELAYD 2>&1 &
		ok $? "Start lttng-relayd (opt: \"$opt\")"
	fi
}

function check_relayd()
{
	local str_date
	if [ -z "$(lttng_pgrep lttng-relayd)" ]; then
		str_date=$(date +%H%M%S-%d%m%Y)

		#diag "Relay daemon died. Starting it again"
		if [ -e "$COREDUMP_FILE" ]; then
			mv "$COREDUMP_FILE" "$COREDUMP_FILE-$str_date"
		fi
		start_relayd
	fi
}

test_stress()
{
	# shellcheck disable=SC2034
	for b in $(seq 1 $NR_LOOP); do
		for a in $(seq 1 $NR_SESSION); do
			create_lttng_session_uri $SESSION_NAME-"$a" net://localhost
			check_sessiond
			check_relayd
			enable_channel_per_uid $SESSION_NAME-"$a" $CHANNEL_NAME
			check_sessiond
			check_relayd
			enable_ust_lttng_event_ok $SESSION_NAME-"$a" $EVENT_NAME
			check_sessiond
			check_relayd
			start_lttng_tracing_ok $SESSION_NAME-"$a"
			check_sessiond
			check_relayd
		done

		for a in $(seq 1 $NR_SESSION); do
			stop_lttng_tracing_ok $SESSION_NAME-"$a"
			check_sessiond
			check_relayd
			destroy_lttng_session_ok $SESSION_NAME-"$a"
			check_sessiond
			check_relayd
		done
	done

	return 0
}

function cleanup()
{
	diag "Cleaning up!"
	kill "${APP_PIDS[@]}"
	wait "${APP_PIDS[@]}" 2>/dev/null
	APP_PIDS=()
	# shellcheck disable=SC2119
	stop_lttng_sessiond
	# shellcheck disable=SC2119
	stop_lttng_relayd
}

function sighandler()
{
	cleanup
	#rm $LOG_FILE_SESSIOND $LOG_FILE_RELAYD
	full_cleanup
}

trap sighandler SIGINT SIGTERM

# Make sure we collect a coredump if possible.
ulimit -c unlimited

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

TRACE_PATH=$(mktemp -d -t tmp.test_multi_sess_per_uid_5app_kill_relayd.XXXXXX)

start_relayd "-o $TRACE_PATH"
start_sessiond

diag "Starting applications launcher"

# Start NR_APP applications script that will spawn apps non stop.
"./$TESTDIR/stress/$LAUNCH_APP" $NR_APP &
APP_PIDS+=(${!})

# Launch the helper script that will randomly kill the relayd at vitam eternam.
"./$TESTDIR/stress/$KILL_RELAYD_HELPER" 1 1 &
APP_PIDS+=(${!})

test_stress
out=$?
if [ $out -ne 0 ]; then
	cleanup
	exit $out
fi

cleanup
rm -rf "${TRACE_PATH:?}/"
rm $LOG_FILE_SESSIOND $LOG_FILE_RELAYD
exit 0
